package com.ctg.payroll2.dao.xlsImport.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ctg.payroll2.bean.po.ExcelPayClass;
import com.ctg.payroll2.bean.po.ExcelPayClassItems;
import com.ctg.payroll2.bean.po.ExcelSocial;
import com.ctg.payroll2.bean.po.exceloperator.PayInput;
import com.ctg.payroll2.bean.po.exceloperator.SocialResult;
import com.ctg.payroll2.dao.xlsImport.XlsPublicToosDAO;
import com.ctg.platform.orm.ibatis.IbatisGenericDao;
import com.ctg.platform.util.Page;

@SuppressWarnings("unchecked")
public class XlsPublicToosDAOImpl extends IbatisGenericDao implements
		XlsPublicToosDAO {

	public Page queryPayClassByCompanyCode(Object paraMap, Integer start,
			Integer limit) {
		return this.pagedQuery(ExcelPayClass.class,
				(Map<String, Object>) paraMap, start, limit);
	}

	public Page queryPayClassItemsByPayClassId(Object paraMap, Integer start,
			Integer limit) {
		return this.pagedQuery(ExcelPayClassItems.class,
				(Map<String, Object>) paraMap, start, limit);
	}

	public Page querySocialByCompanyCode(Object paraMap, Integer start,
			Integer limit) {
		return this.pagedQuery(ExcelSocial.class,
				(Map<String, Object>) paraMap, start, limit);
	}

	public int deletePayInputData(Object paraMap) {
		return this.getSqlMapClientTemplate().delete(
				"ExeclPublicTools.deletePayInput", (String) paraMap);
	}

	public int deleteSocialResult(Object paraMap) {
		return this.getSqlMapClientTemplate().delete(
				"ExeclPublicTools.deleteSocial", (String) paraMap);
	}

	/**
	 * @param countsql
	 * @param querysql
	 * @param paramap
	 * @param start
	 * @param limit
	 * @return
	 */
	public Page PageQuery(String countsql, String querysql, Object paramap,
			Integer start, Integer limit) {
		HashMap<String, Object> pa = (HashMap<String, Object>) paramap;
		Integer totalCount = (Integer) this.getSqlMapClientTemplate()
				.queryForObject(countsql, paramap);
		if (totalCount.intValue() == 0) {
			return new Page();
		}
		List list;
		int totalPageCount = 0;
		int startIndex = 0;
		if (limit > 0) {
			totalPageCount = (totalCount / limit);
			totalPageCount += ((totalCount % limit) > 0) ? 1 : 0;
			if (totalCount >= start) {
				startIndex = start;
			} else {
				startIndex = (totalPageCount - 1) * limit;
			}
			if (pa == null) {
				pa = new HashMap();
			}
			pa.put("startIndex", startIndex);
			pa.put("endIndex", limit);
			list = this.getSqlMapClientTemplate().queryForList(querysql, pa);
		} else {
			list = this.getSqlMapClientTemplate().queryForList(querysql, pa);
		}
		return new Page(startIndex, totalCount, limit, list);
	}

	public Page queryPayInputData(Object paramap, Integer start, Integer limit) {
		return PageQuery("ExeclPublicTools.countPayInput",
				"ExeclPublicTools.queryPayInput", paramap, start, limit);
	}

	public Page querySocialResult(Object paramap, Integer start, Integer limit) {
		return PageQuery("ExeclPublicTools.querySocialCount",
				"ExeclPublicTools.querySocial", paramap, start, limit);
	}

	public Integer queryPayInputDataCount(Object paramap) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"ExeclPublicTools.countPayInput", paramap);
	}

	public Integer querySocialResultCount(Object paramap) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"ExeclPublicTools.querySocialCount", paramap);
	}

	public List<HashMap<String, Object>> payClassIsValid(Object paramap) {
		return this.getSqlMapClientTemplate().queryForList(
				"ExeclPublicTools.payClassIsValid", paramap);
	}

	public List<HashMap<String, Object>> socialIsValid(Object paramap) {
		return this.getSqlMapClientTemplate().queryForList(
				"ExeclPublicTools.socialIsValid", paramap);
	}

}
